// pages/goods/goodsActiveDetail/goodsActiveDetail.js
let app = getApp()
import { requestApi } from '../../../api.js'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailData: {},
    goodsModelList: [],
    topicResource: [],
    index: 0,
    channel: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options.channel || 'wx_xcx'
  
    // Gucci活动结束处理：自动跳转到首页
    wx.switchTab({
      url: '../../index/index?channel=' + channel,
    })
    
    // this.getBrandActiveData(channel)
  },

  getBrandActiveData(channel) {
    app.request({
      url: requestApi.getBrandActivityUrl,
      login: false,
      data: {
        channel: channel
      }
    }, (data) => {
      let detailData

      if (data.original.data) {
        detailData = data.original.data

        if (detailData.top_pic && detailData.top_pic.path) {
          detailData.top_pic.new_path = formatImage.cdnImage(detailData.top_pic.path, formatImage.THUMBNAIL_L)
        }

        if (detailData.topic_resource && detailData.topic_resource.length) {
          detailData.topic_resource.forEach((topicResourceItem) => {
            if (topicResourceItem.banner_image && topicResourceItem.banner_image.path) {
              topicResourceItem.banner_image.new_path = formatImage.cdnImage(topicResourceItem.banner_image.path, formatImage.THUMBNAIL_L)
            }
          })
        }

        if (detailData.goods_model && detailData.goods_model.length) {
          detailData.goods_model.forEach((goodsModelItem) => {
            if (goodsModelItem.pic_list && goodsModelItem.pic_list.length) {
              goodsModelItem.pic_list.forEach((picListItem) => {
                if (picListItem && picListItem.path) {
                  picListItem.new_path = formatImage.cdnImage(picListItem.path, formatImage.THUMBNAIL_L)
                }
              })
            }

            if (goodsModelItem.goods_list && goodsModelItem.goods_list.length) {
              goodsModelItem.goods_list.forEach((goodsListItem) => {
                if (goodsListItem.product_cover_image && goodsListItem.product_cover_image.path) {
                  goodsListItem.product_cover_image.new_path = formatImage.cdnImage(goodsListItem.product_cover_image.path, formatImage.THUMBNAIL_L)
                }

                goodsListItem.goods_price = goodsListItem.goods_price ? formatPrice(goodsListItem.goods_price / 100) : ''
                
              })
            }
          })
        }

        if (detailData.goods_type_model && detailData.goods_type_model.length) {
          detailData.goods_type_model.forEach((modelItem) => {
            if (modelItem && modelItem.length) {
              modelItem.forEach((goodsItem) => {
                if (goodsItem.product_cover_image && goodsItem.product_cover_image.path) {
                  goodsItem.product_cover_image.new_path = formatImage.cdnImage(goodsItem.product_cover_image.path, formatImage.THUMBNAIL_L)
                }

                goodsItem.new_goods_price = goodsItem.goods_price ? formatPrice(goodsItem.goods_price / 100) : ''
              })
            }
          })
        }
      }

      this.setData({
        channel: channel,
        detailData: detailData,
        goodsModelList: detailData.goods_model,
        topicResource: detailData.topic_resource
      })

      wx.setNavigationBarTitle({
        title: detailData.title
      })
    })
  },

  toTopicDetail(e) {
    let tid = e.currentTarget.dataset.id

    wx.navigateTo({
      url: '../../topic/topicDetail/topicDetail?tid='+tid+'&start=0'
    })
  },

  toGoodsDetail(e) {
    let gid = e.currentTarget.dataset.id

    wx.navigateTo({
      url: '../../goods/goodsDetail/goodsDetail?gid=' + gid + '&channel=' + this.data.channel
    })
  },

  goodsChange(e) {
    let index = e.currentTarget.id

    this.setData({
      index: index
    })
  },

  /**
   * 页面分享
   */
  // onShareAppMessage: function() {
  //   return {
  //     title: this.data.detailData.title,
  //     imageUrl: this.data.detailData.top_pic.new_path,
  //     path: '/pages/goods/goodsActiveDetail/goodsActiveDetail'
  //   }
  // }
})